package com.hawthorn.redis.service.bizconf.impl;

import com.hawthorn.component.constant.RedisConstant;
import com.hawthorn.component.utils.common.StrMyUtil;
import com.hawthorn.entity.po.SysBizConfPO;
import com.hawthorn.platform.redis.RedisMyClient;
import com.hawthorn.platform.utils.common.MyUtil;
import com.hawthorn.redis.repository.SysBizConfCacheMapper;
import com.hawthorn.redis.service.bizconf.SysBizConfCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>todo 加载业务参数缓存服务实现类</p>
 * @author: andyten@tom.com | xukaixing@hotmail.com | xukaixing@hotmail.com
 * @created: 2021/11/11 8:48 PM
 * @version v1.0.1
 * @copyright: Copyright (c)  andyten
 */
@Service
@Slf4j
public class SysBizConfCacheServiceImpl implements SysBizConfCacheService
{
  @Autowired
  RedisMyClient redisMyClient;

  @Resource
  private SysBizConfCacheMapper sysBizConfCacheMapper;

  /**
   * <p>todo 初始化方法</p>
   * @param:
   * @return: void
   * @author: andyten@tom.com | xukaixing@hotmail.com | xukaixing@hotmail.com
   * @date: 2021/11/11 8:48 PM
   * @version: 1.0.1
   * Modification History:
   * Date         Author          Version            Description
   * -----------------------------------------------------------
   * 2021/11/11    andy.ten        v1.0.1             init
   */
  @Transactional(readOnly = true)
  public void init()
  {
    List<Map<String, Object>> sysBizConfCache = sysBizConfCacheMapper.selectBizConf();
    String key;
    // todo 先删除redis中的key
    redisMyClient.removeAll(StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_BIZCONF_PREFIX, "*"));

    for (Map bizConf : sysBizConfCache)
    {
      key = StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_BIZCONF_PREFIX, bizConf.get("para_key"));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_BIZCONF_ID, MyUtil.getValue(bizConf.get("id")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_BIZCONF_TYPE, MyUtil.getValue(bizConf.get("type")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_BIZCONF_TYPENAME, MyUtil.getValue(bizConf.get("type_name")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_BIZCONF_PARAKEY, MyUtil.getValue(bizConf.get("para_key")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_BIZCONF_PARANAME, MyUtil.getValue(bizConf.get("para_name")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_BIZCONF_PARAVAL1, MyUtil.getValue(bizConf.get("para_val1")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_BIZCONF_PARAVAL2, MyUtil.getValue(bizConf.get("para_val2")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_BIZCONF_PARAVAL3, MyUtil.getValue(bizConf.get("para_val3")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_BIZCONF_PARAVAL4, MyUtil.getValue(bizConf.get("para_val4")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_BIZCONF_PARAVAL5, MyUtil.getValue(bizConf.get("para_val5")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_BIZCONF_BIZTYPE, MyUtil.getValue(bizConf.get("biz_type")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_BIZCONF_STATUS, MyUtil.getValue(bizConf.get("status")));
    }
    log.info("====== load bizconf to redis success ======");
  }

  @Override
  public void reloadBizConfById(SysBizConfPO sysBizConfPO)
  {
    log.info("====== 执行开始======");
    String key;
    key = StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_BIZCONF_PREFIX, sysBizConfPO.getParaKey());
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_BIZCONF_ID, MyUtil.getValue(sysBizConfPO.getId()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_BIZCONF_TYPE, MyUtil.getValue(sysBizConfPO.getBizType()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_BIZCONF_TYPENAME, sysBizConfPO.getTypeName());
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_BIZCONF_PARAKEY, sysBizConfPO.getParaKey());
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_BIZCONF_PARANAME, sysBizConfPO.getParaName());
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_BIZCONF_PARAVAL1, sysBizConfPO.getParaVal1());
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_BIZCONF_PARAVAL2, sysBizConfPO.getParaVal2());
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_BIZCONF_PARAVAL3, sysBizConfPO.getParaVal3());
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_BIZCONF_PARAVAL4, sysBizConfPO.getParaVal4());
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_BIZCONF_PARAVAL5, sysBizConfPO.getParaVal5());
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_BIZCONF_BIZTYPE, MyUtil.getValue(sysBizConfPO.getBizType()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_BIZCONF_STATUS, MyUtil.getValue(sysBizConfPO.getStatus()));
    
    log.info("====== 执行结束 ======");
  }
}
